﻿using SCG.TurboSprite;
using SCG.WinForms;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TTEngine;

namespace TorchesTombs
{
    public class MapObjectSprite : BitmapSprite
    {
        //constructor
        public MapObjectSprite(MapObject mapObject)
            : base(GetTransparentBitmap(mapObject))
        {
            _mapObject = mapObject;
            if (mapObject != null)
                mapObject.Sprite = this;
        }

        //static constructor
        static MapObjectSprite()
        {
            SelectedPen = new Pen(Color.FromArgb(128, 0, 255, 255));
            SelectedPen.Width = 4;
            MovePen = new Pen(Color.FromArgb(128, 255, 255, 255));
            MovePen.Width = 4;
            EscortPen = new Pen(Color.FromArgb(128, 128, 255, 128));
            EscortPen.Width = 4;
        }

        //public properties
        public MapObject MapObject
        {
            get
            {
                return _mapObject;
            }
        }

        //selected?
        public bool IsSelected { get; set; }

        //mode
        public MoveMode Mode
        {
            get
            {
                return _moveMode;
            }
            set
            {
                if (_moveMode == value)
                    return;
                if (_moveMode == MoveMode.Prone)
                    Bitmap = _standard;
                _moveMode = value;
                if (Mode == MoveMode.Prone)
                {
                    if (_rotated == null)
                    {
                        _standard = Bitmap;
                        _rotated = (Bitmap)ImageUtils.RotateImage(Bitmap, 90);
                    }
                    Bitmap = _rotated;
                }
            }
        }

        //facing
        public Facing Facing { get; set; }

        //"work" location
        public int WorkX { get; set; }
        public int WorkY { get; set; }

        //render
        public override void Render(Graphics g)
        {
            //if not prone, draw right or left
            if (Mode == MoveMode.Prone)
                base.Render(g);
            else
            {
                if (Facing == Facing.Right)
                    base.Render(g);
                else
                    g.DrawImage(Bitmap, X + WidthHalf - Surface.OffsetX, Y - HeightHalf - Surface.OffsetY, -Bitmap.Width, Bitmap.Height);
            }

            //selection border
            if (IsSelected)
            {
                g.DrawRectangle(SelectedPen, X - 32 - Surface.OffsetX, Y - 32 - Surface.OffsetY, 64, 64);
            }

            //draw order lines?
            if (MapObject.IsActor)
            {
                Actor actor = (Actor)MapObject;
                if (actor.IsPC)
                {
                    if (actor.Orders == OrderType.Move)
                    {
                        int x1 = (int)(X - Surface.OffsetX);
                        int y1 = (int)(Y - Surface.OffsetY);
                        int x2 = actor.DestX * 64 - Surface.OffsetX + 32;
                        int y2 = actor.DestY * 64 - Surface.OffsetY + 32;
                        g.DrawLine(MovePen, x1, y1, x2, y2);
                    }
                    else if (actor.Orders == OrderType.Escort)
                    {
                        int x1 = (int)(X - Surface.OffsetX);
                        int y1 = (int)(Y - Surface.OffsetY);
                        Sprite targetSprite = (Sprite)actor.Target.Sprite;
                        int x2 = (int)(targetSprite.X - Surface.OffsetX);
                        int y2 = (int)(targetSprite.Y - Surface.OffsetY);
                        g.DrawLine(EscortPen, x1, y1, x2, y2);
                    }
                }
            }
        }

        //get a transparent bitmap
        public static Bitmap GetTransparentBitmap(MapObject mapObject)
        {
            if (mapObject == null)
                return null;
            if (!TransparentBitmaps.ContainsKey(mapObject.ObjectName))
            {
                Bitmap bmp = mapObject.Image;
                if (bmp == null)
                    return null;
                bmp.MakeTransparent(Color.Fuchsia);
                if (mapObject.ImagesAreUnique)
                    return bmp;
                TransparentBitmaps[mapObject.ObjectName] = bmp;
            }
            return TransparentBitmaps[mapObject.ObjectName];
        }

        //private members
        private MapObject _mapObject;
        private MoveMode _moveMode;
        private static Dictionary<string, Bitmap> TransparentBitmaps = new Dictionary<string, Bitmap>();
        private static Pen SelectedPen;
        private static Pen MovePen;
        private static Pen EscortPen;
        private Bitmap _standard = null;
        private Bitmap _rotated = null;
    }

    //movement modes
    public enum MoveMode { Normal, Lunge, Work, WorkAt, Prone };

    //facing
    public enum Facing { Right, Left };
}
